package com.cgw.game.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

/**
 * @类描述 色块进度条
 * @作者 YF
 * @创建时间 2020/4/17 11:45
 */
public class ShadowView extends View {
    /**
     * 背景画笔
     * */
    private Paint mBgPaint;
    private Paint mBg2Paint;

    /**
     * 宽
     * */
    private int width;
    /**
     * 高
     * */
    private int height;


    /**
     * 上下文对象
     * */
    private Context context;

    /**
     * 左右默认边距
     * */
    private float defaultMargin = 10f;
    private float padding = 8f;





    public ShadowView(Context context) {
        this(context,null);
    }

    public ShadowView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ShadowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr){
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    private void init() {
        mBgPaint = new Paint();
        mBgPaint.setAntiAlias(true);
        mBgPaint.setStyle(Paint.Style.FILL);
        mBgPaint.setColor(Color.WHITE);

        mBg2Paint = new Paint();
        mBg2Paint.setAntiAlias(true);
        mBg2Paint.setStyle(Paint.Style.FILL);
        mBg2Paint.setColor(Color.WHITE);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        width = measureSize(widthMeasureSpec);
        height = width;
        setMeasuredDimension(width,width);
    }
    private int measureSize(int measureSpec) {
        int length;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if(mode == MeasureSpec.EXACTLY){
            length = size;
        }else{
            length = DensityUtil.dip2px(context,120);
            if(mode == MeasureSpec.AT_MOST){
                length = Math.min(length,size);
            }
        }
        return length;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.translate(width / 2, height / 2);
        drawBackGround1(canvas);
//        drawBackGround(canvas);
    }
    float angel = 0;
    private void drawBackGround1(Canvas canvas) {
        float radius = width/2 - DensityUtil.dip2px(context,defaultMargin);
        mBg2Paint.setColor(Color.WHITE);
        canvas.drawCircle(0,0,radius,mBg2Paint);

        radius = radius-DensityUtil.dip2px(context,defaultMargin);
        mBgPaint.setShadowLayer(30, 0, 5, 0xFFccd1d1);
        canvas.drawCircle(0,0,radius,mBgPaint);
        mBg2Paint.setColor(Color.BLACK);



        canvas.save();
        canvas.rotate(angel);
        Path path = new Path();
        path.moveTo(-radius,0);
        path.arcTo(-radius,-radius/2,0,radius/2,
                180,-180,false);
        path.arcTo(0,-radius/2,radius,radius/2,
                180,180,false);
        path.arcTo(-radius,-radius,radius,radius,
                0,180,false);
        path.close();
        canvas.drawPath(path,mBg2Paint);
        canvas.restore();
        canvas.save();


        double cos = Math.cos(angel * Math.PI / 180);
        double sin = Math.sin(angel * Math.PI / 180);
        mBg2Paint.setColor(Color.BLACK);
        canvas.drawCircle((float) (-radius/2*cos),(float) (-radius/2*sin),radius/4,mBg2Paint);

        mBg2Paint.setColor(Color.WHITE);
        canvas.drawCircle((float)(radius/2*cos),(float) (radius/2*sin),radius/4,mBg2Paint);
    }

    private void drawBackGround(Canvas canvas) {
        float radius = width/2 - DensityUtil.dip2px(context,defaultMargin);
        mBg2Paint.setColor(Color.WHITE);
        canvas.drawCircle(0,0,radius,mBg2Paint);


        mBgPaint.setShadowLayer(30, 0, 5, 0xFFccd1d1);

        canvas.drawCircle(0,0,radius-DensityUtil.dip2px(context,defaultMargin),mBgPaint);

        mBg2Paint.setColor(Color.parseColor("#c3c8c8"));


        radius = radius-DensityUtil.dip2px(context,defaultMargin) - DensityUtil.dip2px(context,padding);
        float bgWidth = 15f;
        mBg2Paint.setStrokeCap(Paint.Cap.ROUND);
        mBg2Paint.setStrokeWidth(15f);
        canvas.save();
        canvas.rotate(-90f);
        double angel = 360 / 24;

        for (float i = 0; i < 24; i++) {
            double cos = Math.cos((angel * i) * Math.PI / 180);
            double sin = Math.sin((angel * i) * Math.PI / 180);
            if (i % 4 == 0) {

                canvas.drawLine(
                        (float) (radius  * cos),
                        (float) (radius  * sin),
                        (float) ((radius-bgWidth*3) * cos),
                        (float) ((radius-bgWidth*3) * sin), mBg2Paint);

            }else {
                canvas.drawLine(
                        (float) (radius * cos),
                        (float) (radius * sin),
                        (float) ((radius-bgWidth) * cos),
                        (float) ((radius-bgWidth) * sin), mBg2Paint);
            }

        }
        canvas.restore();
        canvas.save();
    }

    public void setAngel(float angel) {
        startAnimator(angel);
    }

    ValueAnimator animator;
    private void startAnimator(float pro){
        if(animator!=null&&animator.isRunning()){
            animator.cancel();
            animator = null;
        }
        animator = ValueAnimator.ofFloat(pro);
        animator.setDuration(1000);
        animator.setRepeatCount(-1);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                angel = (float)animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.start();
    }
}
